package com.example.demo;

import java.nio.charset.StandardCharsets;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

@Service
public class MessageSubscriber {

    public MessageSubscriber(@Value("${app.configuration.redis.channel}") String channel,
            RedisTemplate<String, String> redisTemplate) {
        RedisConnection redisConnection = redisTemplate.getConnectionFactory().getConnection();
        System.out.println("Receive channel : " + channel);
        redisConnection.subscribe(new MessageListener() {
            @Override
            public void onMessage(Message message, byte[] bytes) {
                // 收到消息的处理逻辑
                System.out.println("Receive message : " + message);
            }
        }, channel.getBytes(StandardCharsets.UTF_8));

    }
}
